#include "commonpage.h"
#include "ui_commonpage.h"

void CommonPage::setMusicListType(PageType pT)
{
    pageType=pT;
}

CommonPage::CommonPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CommonPage)
{
    ui->setupUi(this);
    // 不要⽔平滚动条
    ui->listWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    connect(ui->PlayAll, &QPushButton::clicked, this, [=](){
        emit playAll(pageType);
    });
    connect(ui->listWidget, &QListWidget::doubleClicked, this, [=](const
            QModelIndex &index){
        // ⿏标双击后，发射信号告诉QQMusic，博能放this页⾯中共被双击的歌曲
        emit playMusicByIndex(this, index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setSign(QString sign)
{
    ui->signLabel->setText(sign);
}

void CommonPage::setMidImg(QString Imagepath)
{
    ui->MidLabel->setPixmap(QPixmap(Imagepath));
    ui->MidLabel->setScaledContents(true);
}

void CommonPage::setCommonPageUI(QString sign, QString img_path)
{
    setSign(sign);
    setMidImg(img_path);
}

void CommonPage::reFresh(MusicList &musicList)
{
    //需要先将之前的内容清楚掉，否则就会重复
    ui->listWidget->clear();
    // 从musicList中分离出当前⻚⾯的所有⾳乐
    addMusicToMusicPage(musicList);
    // 遍历歌单，将歌单中的歌曲显⽰到界⾯
    for(auto musicId : musicListOfPage)
    {
        auto it = musicList.findMusicById(musicId); //auto 自动推导的类型是Music
        if(it == musicList.end())
            continue;
        SongItem* listItemBox = new SongItem(ui->listWidget);
        listItemBox->setMusicName(it->getMusicName());
        listItemBox->setSinger(it->getMusicSinger());
        listItemBox->setAlbumName(it->getMusicAlbum());
        listItemBox->setLikeIcon(it->getIsLike());
        listItemBox->islike=it->getIsLike();
        QListWidgetItem* listWidgetItem = new QListWidgetItem(ui->listWidget);
        listWidgetItem->setSizeHint(QSize(ui->listWidget->width(), 45));
        ui->listWidget->setItemWidget(listWidgetItem, listItemBox);

        connect(listItemBox,&SongItem::likeChange,this,[=](bool islike){
            emit ResetLike(islike,it->getMusicId());
        });
    }
    // 更新完成后刷新下界⾯
    ui->listWidget->repaint();
}

void CommonPage::addMusicToPlayer(MusicList &musicList, QMediaPlaylist *playList)
{
    // 根据⾳乐列表中⾳乐所属的页⾯，将⾳乐添加到playList中
    for(auto music : musicList.musicList)
    {
        switch(pageType)
        {
        case LOCAL_PAGE:
        {
            playList->addMedia(music.getMusicUrl());
            break;
        }
        case LIKE_PAGE:
        {
            if(music.getIsLike())
            {
                playList->addMedia(music.getMusicUrl());
            }
            break;
        }
        case HISTORY_PAGE:
        {
            if(music.getIsHistory())
            {
                playList->addMedia(music.getMusicUrl());
            }
            break;
        }
        default:
            break;
        }
    }
}



const QString &CommonPage::getMusicIdByIndex(int index)
{
    if(index >= musicListOfPage.size())
    {
        qDebug()<<"无此歌曲";
        return "";
    }
    return musicListOfPage[index];
}


void CommonPage::addMusicToMusicPage(MusicList &musicList)
{
    // 将旧内容清空
    musicListOfPage.clear();
    for(auto music : musicList.musicList)
    {
        switch(pageType)    //根据commonpage的类型选择music加入
        {
        case LIKE_PAGE:
            if(music.getIsLike())
                musicListOfPage.push_back(music.getMusicId());
            break;
        case LOCAL_PAGE:
            musicListOfPage.push_back(music.getMusicId());
            break;
        case HISTORY_PAGE:
            if(music.getIsHistory())
                musicListOfPage.push_back(music.getMusicId());
            break;
        default:
            qDebug()<<"暂未支持";
        }
    }

}

void CommonPage::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{

}
